#include "MaterialGraphPinModel.h"

#include "MaterialGraph/Node/MaterialGraphNodeModel.h"
#include "MaterialGraph/Link/MaterialGraphLinkModel.h"
CString RMaterialGraphPinModel::GetValueExpression() const
{
    assert(!bIsOutput);
    if(HasValidLink())
    {
        //only allow have one valid link
        //so just use the first valid link
        for(auto& Link : Links)
        {
            if(Link->IsValid())
            {
                return Link->Start->UniqueOutputPinName;
            }
        }
        //unreachable
        assert(false);
        return U"";
    }
    else
    {
        switch(ValueType)
        {
            case EMaterialGraphValueType::Vector1D:
            {
                return CString(DefaultInputX);
            }
            case EMaterialGraphValueType::Vector2D:
            {
                return U"vec2(" + CString(DefaultInputX) + U"," + CString(DefaultInputY) + U")";
            }
            case EMaterialGraphValueType::Vector3D:
            {
                return U"vec3(" + CString(DefaultInputX) + U"," + CString(DefaultInputY) + U"," + CString(DefaultInputZ) + U")";
            }
            case EMaterialGraphValueType::Vector4D:
            {
                return U"vec4(" + CString(DefaultInputX) + U"," + CString(DefaultInputY) + U"," + CString(DefaultInputZ) + U"," + CString(DefaultInputW) + U")";
            }
            default:
            {
                //unreachable
                assert(false);
                return U"";
            }
        }
    }
}

bool RMaterialGraphPinModel::HasValidLink() const
{
    for(auto& Link : Links)
    {
        if(Link->IsValid())
        {
            return true;
        }
    }

    return false;

}

TSharedObjectPtr<RMaterialGraphLinkModel> RMaterialGraphPinModel::GetFirstValidLink() const
{
    for(auto& Link : Links)
    {
        if(Link->IsValid())
        {
            return Link;
        }
    }

    return nullptr;
}

TSharedObjectPtr<RMaterialGraphNodeModel> RMaterialGraphPinModel::GetNode() const
{
    return GetOwner();
}